package com.ql.controller.caiwu;

import com.ql.utils.ExcelUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;

@Controller
@RequestMapping("/caiwu/")
public class CaiwuController {

//    @Resource(name = "reportService")
//    private ReportManager reportService;

    /**
     * 导出报表
     * @return
     */
    @RequestMapping(value = "export")
    @ResponseBody
    public void export(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //获取数据
//        List<PageData> list = reportService.bookList(page);

        //excel标题
       String[] title = {"项目","资金（单位：万）"};

         //excel文件名
         String fileName = "医院财务报表情况"+System.currentTimeMillis()+".xls";

  //sheet名
         String sheetName = "医院财务报表";
//        String [][] content = new String[2][];
        String [][] content ={{"设备支出","335"},{"薪资支出","310"},{"后勤支出","234"},{"药品支出","315"},{"医院收入","1548"}};
//        for (int i = 0; i < List.size(); i++) {
//            content[i] = new String[title.length];
//            Code obj = List.get(i);
//            content[i][0] = String.valueOf(obj.getId());
//            content[i][1] = obj.getCode();
//            content[i][2] = String.valueOf(obj.getCreateTime());
//        }
//        content[0] = new String[5];
//        content[0][0] = "设备支出";//obj.get("stuName").tostring()
//        content[0][1] = "薪资支出";//obj.get("stuSex").tostring()
//        content[0][2] = "后勤支出";//obj.get("stuAge").tostring()
//        content[0][3] = "药品支出";//obj.get("stuSchoolName").tostring()
//        content[0][4] ="医院收入" ;//obj.get("stuClassName").tostring()
//            for (int i = 0; i < list.size(); i++) {
//            content[1] = new String[5];
//            PageData obj = list.get(i);
//            content[1][0] = "335";//obj.get("stuName").tostring()
//            content[1][1] = "310";//obj.get("stuSex").tostring()
//            content[1][2] = "234";//obj.get("stuAge").tostring()
//            content[1][3] = "315";//obj.get("stuSchoolName").tostring()
//            content[1][4] ="1548" ;//obj.get("stuClassName").tostring()
////                }

            //创建HSSFWorkbook
            HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(sheetName, title, content, null);

            //响应到客户端
            try {
            this.setResponseHeader(response, fileName);
            OutputStream os = response.getOutputStream();
            wb.write(os);
            os.flush();
            os.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
    }

    //发送响应流方法
    public void setResponseHeader(HttpServletResponse response, String fileName) {
        try {
            try {
                fileName = new String(fileName.getBytes(),"ISO8859-1");
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            response.setContentType("application/octet-stream;charset=ISO8859-1");
            response.setHeader("Content-Disposition", "attachment;filename="+ fileName);
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

}
